﻿//主界面的Grid
var LibraryMain = new Ext.data.Store
(
   {
       //表示从哪里获得数据集
       proxy: new Ext.data.HttpProxy
     (
        {
            url: '/WebAppLibrary/Json/MianGrid.aspx'
        }
     ),
       //解析Json
       reader: new Ext.data.JsonReader
     (
        {
            root: 'data',
            id: 'BorrowBookID',
            fields:
           [
              'BorrowBookID', 'BookId', 'BookName', 'BookNumber', 'TypeName', 'StateName', 'BookAuther', 'BookPublicTime', 'BookPrice', 'IsDisk', 'Remark'
           ]
        }
     ),
       remoteSort: true
   }
);
Ext.onReady
(
  function() {
      var colModel = new Ext.grid.ColumnModel
    (
       [
          { header: "图书号", width: 60, dataIndex: 'BookId' },
          { header: "图书名称", width: 60, dataIndex: 'BookName' },
          { header: "图书编号", width: 60, dataIndex: 'BookNumber' },
          { header: "图书类型", width: 80, dataIndex: 'TypeName' },
          { header: "出版时间", width: 85, dataIndex: 'BookPublicTime' },
          { header: "借阅状态", width: 80, dataIndex: 'StateName' },
          { header: "图书金额", width: 80, renderer: getColor, dataIndex: 'BookPrice' },
          { header: "是否有光盘", width: 120, dataIndex: 'IsDisk' },
          { header: "备注", width: 120, dataIndex: 'Remark' }
       ]
    );
      //设置金额颜色
      function getColor(val) {
          if (val != "") {
              return '<span style="color:red;">' + '$' + val + '</span>';
          }
      }

      var grid = new Ext.grid.GridPanel
      (
         {
             renderTo: 'grid',
             height: 500,
             width: 665,
             cm: colModel, //行列
             store: LibraryMain, //数据源
             trackMouseOver: true, //鼠标特效
             loadMask: true,
             autoShow: true,
             autoScroll: true,
             //头部
             tbar:
                [
                    '图书查询',
                    { xtype: 'textfield', width: 170, id: 'title', name: 'title' },
                    { text: '搜索', iconCls: 'search', handler: SerachGrid }, { xtype: 'tbseparator' }
                ]
         }
      )


      //为右键菜单添加事件监听器
      grid.addListener('rowcontextmenu', rightClickFn);
      var rightClick = new Ext.menu.Menu
        (
            {
                id: 'rightClickCont',
                items:
                [
                    {
                        id: 'rMenu1',
                        handler: OpenBook, //点击后触发的事件
                        text: '新借图书'
                    },
                    {
                        id: 'rMenu2',
                        text: '归还图书',
                        handler: function() {
                            var row = grid.getSelections();
                            if (row.length > 0) {
                                var OpenBookId = row[0].get('OpenBookId');
                                var BookId = row[0].get('BookId');
                                var BookName = row[0].get('BookName');
                                var BookNumber = row[0].get('BookNumber');
                                var TypeID = row[0].get('TypeID');
                                var BookPublicTime = row[0].get('BookPublicTime');
                                var StateID = row[0].get('StateID');
                                var BookPrice = row[0].get('BookPrice');
                                var IsDisk = row[0].get('IsDisk');
                                var Remark = row[0].get('Remark');
                                //调用还书的方法
                                CloseBook(OpenBookId, BookId, BookName, BookNumber, TypeID, BookPublicTime, StateID, BookPrice, IsDisk, Remark);
                            }
                            else {
                                Ext.MessageBox.alert('警告', '请选择归还的图书!');
                            }
                        }
                    }
                ]
            }
        );

      function rightClickFn(grid, rowIndex, e) {
          e.preventDefault();
          rightClick.showAt(e.getXY());
      }
      //加载数据
      LibraryMain.load();
  }
);

function SerachGrid() {
    var message = Ext.get('title').dom.value;
    LibraryMain.reload
    (
        {
            params: { msg: message }
        }
    )
}